// Copyright (c) 2008 Matthew Johnson
// Licensed under the MIT License (see license.txt)
package actionml.geom {
	import actionml.Global;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class RectangleAccessorMethods {
		public static function initialize() {
			Global.initializeExtension(RectangleAccessorMethods, Rectangle);
		}
		public static var instanceMethods:Object = {
			getSize: function() {
				return new Point(this.width, this.height);
			},
			getArea: function() {
				return this.width * this.height;
			},
			getCenterX: function() {
				return this.left + this.width / 2;
			},
			getCenterY: function() {
				return this.top + this.height / 2;
			},
			getCenter: function() {
				return new Point(this.getCenterX(), this.getCenterY());
			},
			getLeft: function() {
				if (this.width >= 0) {
					return this.left;
				} else {
					return this.right;
				}
			},
			getRight: function() {
				if (this.width >= 0) {
					return this.right;
				} else {
					return this.left;
				}
			},
			getTop: function() {
				if (this.height >= 0) {
					return this.top;
				} else {
					return this.bottom;
				}
			},
			getBottom: function() {
				if (this.height >= 0) {
					return this.bottom;
				} else {
					return this.top;
				}
			},
			getTopLeft: function() {
				return new Point(this.getLeft(), this.getTop());
			},
			getTopRight: function() {
				return new Point(this.getRight(), this.getTop());
			},
			getBottomLeft: function() {
				return new Point(this.getLeft(), this.getBottom());
			},
			getBottomRight: function() {
				return new Point(this.getRight(), this.getBottom());
			},

			setSize: function(p:Point) {
				this.width = p.x;
				this.height = p.y;
			},	
			move: function(p:Point) {
				this.x += p.x;
				this.y += p.y;
			},
			setCenterX: function(x:Number) {
				this.x = x - this.width / 2;
				return this;
			},
			setCenterY: function(y:Number) {
				this.y = y - this.height / 2;
				return this;
			},
			setCenter: function(x:Number, y:Number) {
				this.setCenterX(x);
				this.setCenterY(y);
				return this;
			},
			setCenterPoint: function(p:Point) {
				this.setCenterX(p.x);
				this.setCenterY(p.y);
				return this;
			},
			setLeft: function(l:Number) {
				if (this.width >= 0) {
					this.x = l
				} else {
					this.x = l - this.width;
				}
				return this;
			},
			setRight: function(r:Number) {
				if (this.width >= 0) {
					this.x = r - this.width;
				} else {
					this.x = r;
				}
				return this;
			},
			setTop: function(t:Number) {
				if (this.height >= 0) {
					this.y = t;
				} else {
					this.y = t - this.height;
				}
				return this;
			},
			setBottom: function(b:Number) {
				if (this.height >= 0) {
					this.y = b - this.height;
				} else {
					this.y = b;
				}
				return this;
			},
			setTopLeft: function(t:Number, l:Number) {
				this.setTop(t);
				this.setLeft(l);
				return this;
			},
			setTopLeftPoint: function(p:Point) {
				this.setTop(p.y);
				this.setLeft(p.x);
				return this;
			},
			setTopRight: function(t:Number, r:Number) {
				this.setTop(t);
				this.setRight(r);
				return this;
			},
			setTopRightPoint: function(p:Point) {
				this.setTop(p.y);
				this.setRight(p.x);
				return this;
			},			
			setBottomLeft: function(b:Number, l:Number) {
				this.setBottom(b);
				this.setLeft(l);
				return this;
			},
			setBottomLeftPoint: function(p:Point) {
				this.setBottom(p.y);
				this.setLeft(p.x);
				return this;
			},
			setBottomRight: function(b:Number, r:Number) {
				this.setBottom(b);
				this.setRight(r);
				return this;
			},
			setBottomRightPoint: function(p:Point) {
				this.setBottom(p.y);
				this.setRight(p.x);
				return this;
			},

			stretchCenterX: function(x:Number) {
				this.width = (x - this.left) * 2;
				return this;
			},
			stretchCenterY: function(y:Number) {
				this.height = (y - this.top) * 2;
				return this;
			},
			stretchCenter: function(x:Number, y:Number) {
				this.stretchCenterX(x);
				this.stretchCenterY(y);
				return this;
			},
			stretchCenterPoint: function(p:Point) {
				this.stretchCenterX(p.x);
				this.stretchCenterY(p.y);
				return this;
			},
			stretchLeft: function(l:Number) {
				if (this.width >= 0) {
					this.left = l
				} else {
					this.right = l;
				}
				return this;
			},
			stretchRight: function(r:Number) {
				if (this.width >= 0) {
					this.right = r;
				} else {
					this.left = r;
				}
				return this;
			},
			stretchTop: function(t:Number) {
				if (this.height >= 0) {
					this.top = t;
				} else {
					this.bottom = t;
				}
				return this;
			},
			stretchBottom: function(b:Number) {
				if (this.height >= 0) {
					this.bottom = b;
				} else {
					this.top = b;
				}
				return this;
			},
			stretchTopLeft: function(t:Number, l:Number) {
				this.stretchTop(t);
				this.stretchLeft(l);
				return this;
			},
			stretchTopLeftPoint: function(p:Point) {
				this.stretchTop(p.y);
				this.stretchLeft(p.x);
				return this;
			},
			stretchTopRight: function(t:Number, r:Number) {
				this.stretchTop(t);
				this.stretchRight(r);
				return this;
			},
			stretchTopRightPoint: function(p:Point) {
				this.stretchTop(p.y);
				this.stretchRight(p.x);
				return this;
			},			
			stretchBottomLeft: function(b:Number, l:Number) {
				this.stretchBottom(b);
				this.stretchLeft(l);
				return this;
			},
			stretchBottomLeftPoint: function(p:Point) {
				this.stretchBottom(p.y);
				this.stretchLeft(p.x);
				return this;
			},
			stretchBottomRight: function(b:Number, r:Number) {
				this.stretchBottom(b);
				this.stretchRight(r);
				return this;
			},
			stretchBottomRightPoint: function(p:Point) {
				this.stretchBottom(p.y);
				this.stretchRight(p.x);
				return this;
			}
		};
	}
}